const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
const router = new Router()
const cors = require('koa2-cors')
const koaBody = require('koa-body')

const ENV = 'wx-text-1i31b'
// const ENV  = 'number-grwak'

//跨域
app.use(cors({
    origin: ['http://localhost:9528'],
    credentials: true
}))

// 接收post参数解析
app.use(koaBody({
    multipart: true,
}))

app.use(async (ctx, next) => {
    console.log('全局中间件')
    // ctx.body = 'Hello Wolrd'
    ctx.state.env = ENV
    await next()
})

const blog = require('./controller/blog.js')
// const swiper = require('./controller/swiper.js')
const banner = require('./controller/banner.js')
const video = require('./controller/video.js')
const userlist = require('./controller/userlist.js')
const topic = require('./controller/topic.js')
const gift = require('./controller/gift.js')
const order = require('./controller/order.js')

router.use('/blog', blog.routes())
// router.use('/swiper', swiper.routes())
router.use('/banner', banner.routes())
router.use('/video', video.routes())
router.use('/userlist', userlist.routes())
router.use('/topic', topic.routes())
router.use('/gift', gift.routes())
router.use('/order', order.routes())

app.use(router.routes())
app.use(router.allowedMethods())

app.listen(3000, () => {
    console.log('服务器已启动：localhost:3000')
})